﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Diagnostics;


namespace Auction_Boxing_2
{

    public class StateMoving : State
    {
        Animation Stopped;
        Animation Moving;

        public StateMoving()
        {
            StateName = "moving";

        }

        public StateMoving(State state)
        {
            StateName = "moving";
            this.ATextures = state.StatePlayer.ATextures;
            LoadState(state.StatePlayer, ATextures);

        }

        public override void LoadState(BoxingPlayer player, Dictionary<string, Animation> ATextures)
        {
            this.StatePlayer = player;
            this.ATextures = ATextures;
            this.PlayerAnimation = ATextures[StateName];
            this.Stopped = ATextures["stopped"];
            this.Moving = ATextures["moving"];

            this.speed = new Vector2(Tools.speedCalc(StatePlayer.MaxMovement), Tools.Y_SCALE * Tools.speedCalc(StatePlayer.MaxMovement));

            //StatePlayer.Hurtbox = new Rectangle((int)StatePlayer.Position.X, (int)StatePlayer.Position.Y, StatePlayer.Width, StatePlayer.Height);

            StatePlayer.isAttacking = false;
            StatePlayer.isHit = false;

        }
        public override void Initialize()
        {



        }

        public override void Update(GameTime gameTime)
        {
            //StatePlayer.Hurtbox = new Rectangle((int)StatePlayer.Position.X, (int)StatePlayer.Position.Y, StatePlayer.Width, StatePlayer.Height);

            if (areKeysDown(KeyPressed.Up, KeyPressed.Down) || areKeysDown(KeyPressed.Right, KeyPressed.Left))
                PlayerAnimation = Stopped;
            else if (isOneOfTheseDown(KeyPressed.Right, KeyPressed.Left, KeyPressed.Up, KeyPressed.Down))
                PlayerAnimation = Moving;
            else
                PlayerAnimation = Stopped;

        }

        public override void HandleMovement()
        {

            if (StatePlayer.KeysDown.Contains(KeyPressed.Up) && !StatePlayer.KeysDown.Contains(KeyPressed.Right) && !StatePlayer.KeysDown.Contains(KeyPressed.Left))
                Translate(0, -speed.Y);

            if (StatePlayer.KeysDown.Contains(KeyPressed.Down) && !StatePlayer.KeysDown.Contains(KeyPressed.Right) && !StatePlayer.KeysDown.Contains(KeyPressed.Left))
                Translate(0, speed.Y);

            if (StatePlayer.KeysDown.Contains(KeyPressed.Right) && !StatePlayer.KeysDown.Contains(KeyPressed.Up) && !StatePlayer.KeysDown.Contains(KeyPressed.Down))
                Translate(speed.X, 0);

            if (StatePlayer.KeysDown.Contains(KeyPressed.Left) && !StatePlayer.KeysDown.Contains(KeyPressed.Up) && !StatePlayer.KeysDown.Contains(KeyPressed.Down))
                Translate(-speed.X, 0);

            if (StatePlayer.KeysDown.Contains(KeyPressed.Up) && StatePlayer.KeysDown.Contains(KeyPressed.Right))
                Translate(speed.X, -speed.Y);

            if (StatePlayer.KeysDown.Contains(KeyPressed.Up) && StatePlayer.KeysDown.Contains(KeyPressed.Left))
                Translate(-speed.X, -speed.Y);

            if (StatePlayer.KeysDown.Contains(KeyPressed.Down) && StatePlayer.KeysDown.Contains(KeyPressed.Right))
                Translate(speed.X, speed.Y);

            if (StatePlayer.KeysDown.Contains(KeyPressed.Down) && StatePlayer.KeysDown.Contains(KeyPressed.Left))
                Translate(-speed.X, speed.Y);

        }

        public override void HandleDirection()
        {

            if (StatePlayer.KeysDown.Contains(KeyPressed.Right) && !StatePlayer.KeysDown.Contains(KeyPressed.Left))
            {
                StatePlayer.PlayerEffect = SpriteEffects.None;
                StatePlayer.direction = DirectionType.Right;
            }
            if (StatePlayer.KeysDown.Contains(KeyPressed.Left) && !StatePlayer.KeysDown.Contains(KeyPressed.Right))
            {
                StatePlayer.PlayerEffect = SpriteEffects.FlipHorizontally;
                StatePlayer.direction = DirectionType.Left;
            }
        }

        public override void HandleState()
        {
            if (StatePlayer.CurrentHealth <= 0)
                StatePlayer.InternalState = new StateDead(this);

            //else if (StatePlayer.isHit)
                //StatePlayer.InternalState = new StateHit(this);

            else if (StatePlayer.KeysDown.Contains(KeyPressed.Up) && StatePlayer.DblJumpTimer.Active)
                StatePlayer.InternalState = new StateJumping(StatePlayer.direction, this);

            else if (StatePlayer.KeysDown.Contains(KeyPressed.Right) && StatePlayer.DashTimerRight.Active)
                StatePlayer.InternalState = new StateDashing(DirectionType.Right, this);

            else if (StatePlayer.KeysDown.Contains(KeyPressed.Left) && StatePlayer.DashTimerLeft.Active)
                StatePlayer.InternalState = new StateDashing(DirectionType.Left, this);

            else if (StatePlayer.KeysDown.Contains(KeyPressed.Attack0))
            {
                StatePlayer.UseItemEvent(0); // State changes in this function based on the item
            }
            else if (StatePlayer.KeysDown.Contains(KeyPressed.Attack1))
            {
                StatePlayer.UseItemEvent(1); // State changes in this function based on the item
            }
            else if (StatePlayer.KeysDown.Contains(KeyPressed.Attack2))
            {
                StatePlayer.UseItemEvent(2); // State changes in this function based on the item
            }
            else if (StatePlayer.KeysDown.Contains(KeyPressed.Attack3))
            {
                StatePlayer.UseItemEvent(3); // State changes in this function based on the item
            }
        }

        public override void Translate(float x, float y)
        {
            Vector3 Temp = StatePlayer.Position;
            Temp.X += x;
            Temp.Y += y;
            StatePlayer.Position = Temp;

        }

        public override void HandleCollision(List<BoxingPlayer> Players)
        {

        }
    }
}
